<?php
namespace app\api\controller\service;

use app\common\controller\Api;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Log;
class Crontab extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 更新服务者时间
     * @return void
     */
    public function skillTimeUpdate()
    {
        $starttime = strtotime(date("Y-m-d", time()));
        $where['changetime'] = ['<', $starttime];
        $skilltime = new \app\api\model\service\SkillTime();
        $skilltime->where($where)
            ->where('timetype', 'in', [1, 2])
            ->dec('timetype', 1)
            ->update(['changetime' => time()]);
        $skilltime->where($where)
            ->where('timetype', 0)
            ->inc('starttime', 259200)
            ->inc('endtime', 259200)
            ->update(['timetype' => 2, 'state' => 0, 'changetime' => time()]);
        $this->success('时间已更新');
    }

    /**
     * 结算订单
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateOrderSettle()
    {
        $list = \app\api\model\service\Order::where(['is_settle'=>1,'settletime'=>['<',time()]])->field('id,user_id,traveltype,skill_id,shop_id,payprice,act_travel_price,settle_price,skill_percent,shop_percent')->select();
        foreach ($list as $value)
        {
            $res = \app\api\model\service\Rebate::orderRebate($value);
            \app\api\model\service\Order::where(['id'=>$value['id']])->update(['is_settle'=>2,'updatetime'=>time()]);
        }
        $this->success('订单已结算');
    }


    /**
     * 默认好评
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws \think\Exception
     */
    public function commentOrder()
    {
        $commentConfig = \app\api\model\service\ProjectConfig::getProjectConfig('comment_day');
        $list = \app\api\model\service\Order::where(['status' => 6, 'finishtime' => ['<', time() - 86400 * $commentConfig]])->field('id,user_id')->select();
        foreach ($list as $value)
        {
            \app\api\model\service\Comment::commentAdd(['order_id'=>$value['id'],'user_id'=>$value['user_id'],'score'=>5,'content'=>'此用户未填写评价内容','images'=>'']);
        }
        $this->success('订单已处理');
    }

    /**
     * 更新优惠券状态
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function updateCoupon()
    {
        $userCoupon = \app\api\model\service\UserCoupon::where(['state'=>0,'exittime'=>['<',time()]])->field('id')->select();

        foreach ($userCoupon as $value)
        {
            \app\api\model\service\UserCoupon::where('id',$value['id'])->update(['state'=>-1]);
        }
        $this->success('已处理');
    }

    /**
     * 更新会员状态
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function updateData()
    {
        $list = \app\api\model\service\UserInfo::where(['is_plus'=>1,'plustime'=>['<',time()]])->field('id')->select();
        foreach ($list as $value)
        {
            \app\api\model\service\UserInfo::where('id',$value['id'])->update(['is_plus'=>0,'plustime'=>time(),'discount'=>100]);
        }
        $cancelConfig = \app\api\model\service\ProjectConfig::getProjectConfig('cancel_minute');
        $list = \app\api\model\service\Order::where(['status' => 0, 'createtime' => ['<', time() - 60 * $cancelConfig]])->field('id,user_id')->select();
        foreach ($list as $value)
        {
            \app\api\model\service\Order::where(['id'=>$value['id']])->update(['status'=>-1,'updatetime'=>time(),'is_pool'=>0]);
        }
        $this->success('信息已更新');
    }

    /**
     * 统计每日数据
     * @return void
     */
    public function updateTotalData()
    {
        \app\api\model\service\TotalData::totalData(1);
        $this->success('信息已统计');
    }




}
